package edu.uoc.tdp.i18n;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * @author fjaumot
 */
public class TDPLanguageUtils {
    private static ResourceBundle resourceBundle;
    private static String currentBaseName;
    private static Locale currentLocale;
    
    
    public static synchronized boolean setDefaultLanguage(String baseName) {
        try{
            currentBaseName = baseName;
            resourceBundle = ResourceBundle.getBundle(baseName, Locale.getDefault());
            //resourceBundle = ResourceBundle.getBundle(baseName, Locale.ENGLISH);
            return true;
        }catch(MissingResourceException e){
            return false;
        }
    }
    
    public static synchronized boolean setLanguage(String baseName, Locale locale) { 
        try {
                currentBaseName = baseName;
                currentLocale = locale;
                resourceBundle = ResourceBundle.getBundle(baseName,locale);
                return true;
        }catch(MissingResourceException e){
                return false;
        }
    }
    
    public static String getMessage(String key)  {		
        if(resourceBundle != null & resourceBundle.containsKey(key))
            return resourceBundle.getString(key);
        else
            return new String();	
    }
    
    public static String getCurrentBaseName() {
        return currentBaseName;
    }
    
    public static Locale getCurrentLocale() {
        return currentLocale;
    }
}
